<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="showAddRoleDialog(1,'0')">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button v-if="row.type===1" size="small" type="text" @click="showAddRoleDialog(2,row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="showEditRoleDialog(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="delPermission(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 新增编辑的弹层 -->
      <el-dialog :visible="showDialog" :title="showTitle" :close-on-click-modal="false" @close="hideAddRoleDialog">
        <!-- el-form: -->
        <!-- el-form-item: -->
        <!-- el-input: -->
        <!-- 表单内容 -->
        <el-form ref="addForm" label-width="100px" :model="formData" :rules="rules">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="hideAddRoleDialog">取消</el-button>
            <el-button type="primary" @click="confirAddPermission">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissionListApi, addPermissionApi, delPermissionApi, getPermissionDetailApi, editPermissionDetailApi } from '@/api/permisssion'
import { transListToTreeData } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      showDialog: false,
      formData: {
        name: '',
        code: '',
        description: '',
        enVisible: '0',
        type: 1,
        pid: ''
      },
      rules: {
        name: [{ required: true, message: '权限名字不可为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不可为空', trigger: 'blur' }]
      },
      showTitle: ''
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const { data } = await getPermissionListApi()
      this.list = transListToTreeData(data, '0')
    },
    /**
     * 打开添加弹框
     */
    showAddRoleDialog(type, pid) {
      this.formData.type = type
      this.formData.pid = pid
      this.showDialog = true
      this.showTitle = '添加权限'
    },
    /**
     * 关闭弹框
     */
    hideAddRoleDialog() {
      this.showDialog = false
      // 清空数据（prop）和状态
      this.$refs.addForm.resetFields()
      this.formData = {
        name: '',
        code: '',
        description: '',
        enVisible: '0',
        type: 1,
        pid: ''
      }
    },
    /**
     * 确定添加按钮
     */
    confirAddPermission() {
      this.$refs.addForm.validate(async flag => {
        if (!flag) return
        if (this.formData.id) {
          await editPermissionDetailApi(this.formData)
          // 提交成功之后
          // 1.提示
          this.$message.success('恭喜！修改成功')
        } else {
          // 可以提交接口了
          await addPermissionApi(this.formData)
          // 提交成功之后
          // 1.提示
          this.$message.success('恭喜！添加成功')
        }
        // 2.关闭窗口（清空数据）
        this.hideAddRoleDialog()
        // 3.重新渲染
        this.getPermissionList()
      })
    },
    /**
     *删除权限
     */
    delPermission(id) {
      this.$confirm('亲！您确认删除吗？', '提示').then(async() => {
        await delPermissionApi(id)
        this.$message.success('删除权限成功')
        this.getPermissionList()
      }).catch(() => {})
    },
    /**
     * 展示修改弹框
     */
    async showEditRoleDialog(id) {
      const { data } = await getPermissionDetailApi(id)
      this.formData = data
      this.showDialog = true
      this.showTitle = '修改权限'
    }
  }
}
</script>
